package com.yld.car.market;

import java.util.HashMap;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.UserInfo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FeedBackActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.setting_feedback);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		showBackButton();

		((TextView) findViewById(R.id.title)).setText("意见反馈");
		Button localButton = (Button) findViewById(R.id.rightButton);
		localButton.setVisibility(View.VISIBLE);
		localButton.setText("完成");

		localButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				EditText editText = (EditText) findViewById(R.id.feedback);

				final String str1 = editText.getText().toString();

				if (str1.trim().length() == 0) {
					Toast.makeText(FeedBackActivity.this, "请输入你的宝贵建议和意见！",
							Toast.LENGTH_SHORT).show();

					return;

				}

				/**
				 * 
				 FeedbackInfo(string userId, string messages)
				 * 参数一：userId:当前用户ID 参数二：messages: 反馈信息
				 * 
				 * 
				 * 成功，返回：
				 * 
				 * { "result":"1", "returnval":"操作成功，感谢您的意见反馈！" }
				 */
				AsyncTask<String, String, String> task = new AsyncTask<String, String, String>() {

					@Override
					protected String doInBackground(String... params) {

						NetworkProgressUtils utils = NetworkProgressUtils
								.getInstance();
						HashMap<String, String> allParams = new HashMap<String, String>();

						UserInfo u = (UserInfo) readObject("userInfo");
						allParams.put("userId", u.getUserId());
						allParams.put("messages", str1);

						publishProgress("");
						Object o = utils.getWebservicesByData(
								ConstantUtils.NAME_SPACE,
								ConstantUtils.ALL_METHROD_NAME.get(27),
								ConstantUtils.NAME_SPACE
										+ ConstantUtils.ALL_METHROD_NAME
												.get(27),
								ConstantUtils.GET_UPDATE_SELF_PASSWORD_URL,
								allParams);

						if (o != null) {
							return o.toString();
						}
						return null;
					}

					@Override
					protected void onProgressUpdate(String... values) {

						showLoading("正在提交......");
						super.onProgressUpdate(values);
					}

					@Override
					protected void onPostExecute(String result) {
						hideLoading();
						if (result != null && result.indexOf("操作成功") >= 0) {

							showDialogFinish("您的建议已提交成功", true);
						}
						super.onPostExecute(result);
					}
				};

				task.execute("");

			}
		});

	}
}
